package com.qianxx.component.tool;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianxx.component.R;

/**
 * 标题栏
 */
public class TitleBar extends RelativeLayout implements View.OnClickListener {
    private String leftText, centerText, rightText;
    private int leftDrawable, centerDrawable, rightDrawable;
    private TextView left, center, right;
    private OnClickListener onClickListener;

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        leftText = a.getString(R.styleable.TitleBar_LeftText);
        centerText = a.getString(R.styleable.TitleBar_CenterText);
        rightText = a.getString(R.styleable.TitleBar_RightText);
        leftDrawable = a.getResourceId(R.styleable.TitleBar_LeftDrawable, 0);
        centerDrawable = a.getResourceId(R.styleable.TitleBar_CenterDrawable, 0);
        rightDrawable = a.getResourceId(R.styleable.TitleBar_RightDrawable, 0);
        a.recycle();
        setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.title_bar, null);
        left = view.findViewById(R.id.left);
        center = view.findViewById(R.id.center);
        right = view.findViewById(R.id.right);
        left.setText(leftText);
        center.setText(centerText);
        right.setText(rightText);
        left.setOnClickListener(this);
        right.setOnClickListener(this);
        left.setOnClickListener(this);
        left.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, 0, 0);
        center.setCompoundDrawablesWithIntrinsicBounds(centerDrawable, 0, 0, 0);
        right.setCompoundDrawablesWithIntrinsicBounds(rightDrawable, 0, 0, 0);
        addView(view);
    }

    public void setLeftText(String leftText) {
        left.setText(leftText);
    }

    public void setCenterText(String centerText) {
        center.setText(centerText);
    }

    public void setRightText(String rightText) {
        right.setText(rightText);
    }

    public void setLeftRes(int resId) {
        left.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }

    public void setCenterRes(int resId) {
        center.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }

    public void setRightRes(int resId) {
        right.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }

    public void setLeftVisiable(boolean visiable) {
        left.setVisibility(visiable ? VISIBLE : GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left:
                if (onClickListener != null) {
                    onClickListener.onLeftClick(v);
                }
                break;
            case R.id.center:
                if (onClickListener != null) {
                    onClickListener.onCenterClick(v);
                }
                break;
            case R.id.right:
                if (onClickListener != null) {
                    onClickListener.onRightClick(v);
                }
                break;
        }
    }

    public interface OnClickListener {
        void onLeftClick(View v);

        void onCenterClick(View v);

        void onRightClick(View v);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public TextView getLeftTextView() {
        return left;
    }

    public TextView getRightTextView() {
        return right;
    }

    public TextView getCenterTextView() {
        return center;
    }
}
